package com.yvon.maple.cloud.api.system.feign;

import cn.hutool.core.collection.CollUtil;
import com.yvon.boot.common.result.GlobalException;
import com.yvon.boot.common.result.ResponseData;
import com.yvon.maple.cloud.api.system.dto.SecurityUserDTO;
import com.yvon.maple.cloud.api.system.feign.factory.UserFeignFallbackFactory;
import com.yvon.maple.cloud.api.system.vo.LdapUserVo;
import com.yvon.maple.constants.ServiceNameConstant;
import com.yvon.maple.constants.SystemConstant;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author : Yvon
 * @since : 2021-07-26
 */
@FeignClient(value = ServiceNameConstant.SYSTEM_SERVICE, fallbackFactory = UserFeignFallbackFactory.class)
public interface UserFeignClient {

    String PATH = SystemConstant.Common.FEIGN_INSIDE_URL_PREFIX + "/users";

    /**
     * 通过账号查询用户信息.
     *
     * @param username 用户名
     * @return ResponseData<SecurityUserDTO>
     * @author : Yvon / 2022-02-09
     */
    @GetMapping(PATH + "/security/username/{username}")
    ResponseData<SecurityUserDTO> getByUsername(@PathVariable("username") String username);


    /**
     * 通过手机号查询用户信息.
     *
     * @param mobile 移动
     * @return ResponseData<SecurityUserDTO>
     * @author : Yvon / 2022-02-09
     */
    @GetMapping(PATH +"/security/mobile/{mobile}")
    ResponseData<SecurityUserDTO> getByMobile(@PathVariable("mobile") String mobile);


    /**
     * 通过电子邮件查询用户信息.
     *
     * @param email 电子邮件
     * @return ResponseData<SecurityUserDTO>
     * @author : Yvon / 2022-02-09
     */
    @GetMapping(PATH +"/security/email/{email}")
    ResponseData<SecurityUserDTO> getByEmail(@PathVariable("email") String email);


    /**
     * 保存ldap用户
     *
     * @param ldapUserVo ldap用户参数
     * @return ResponseData<Boolean>
     * @author : Yvon / 2022-02-25
     */
    @PostMapping(PATH +"/ldap/save")
    ResponseData<Boolean> saveLdapUser(@RequestBody LdapUserVo ldapUserVo);


    /**
     * 通过用户名查询电子邮件
     *
     * @param userIds 用户名
     * @return ResponseData<Set < String>>
     * @author : Yvon / 2022-02-09
     */
    @PostMapping(PATH +"/msg/userId/email")
    ResponseData<Set<String>> getEmailByUserIds(@RequestBody Set<Long> userIds);



    /**
     * 通过用户名查询电话
     *
     * @param userIds 用户名
     * @return ResponseData<Set < String>>
     * @author : Yvon / 2022-02-09
     */
    @PostMapping(PATH +"/msg/userId/mobile")
    ResponseData<Set<String>> getMobileByUserIds(@RequestBody Set<Long> userIds);


    /**
     * 通过角色代码查询用户名
     *
     * @param roleIds 角色代码
     * @return ResponseData<Set < String>>
     * @author : Yvon / 2022-05-23
     */
    @PostMapping(PATH +"/msg/roleId/userId")
    ResponseData<Set<String>> getUserIdsByRoleIds(@RequestBody Set<Long> roleIds);


    /**
     * 通过角色代码查询电子邮件
     *
     * @param roleIds 用户名
     * @return ResponseData<Set < String>>
     * @author : Yvon / 2022-02-09
     */
    @PostMapping(PATH +"/msg/roleId/email")
    ResponseData<Set<String>> getEmailByRoleIds(@RequestBody Set<Long> roleIds);



    /**
     * 通过角色代码查询电话
     *
     * @param roleIds 用户名
     * @return ResponseData<Set < String>>
     * @author : Yvon / 2022-02-09
     */
    @PostMapping(PATH +"/msg/roleId/mobile")
    ResponseData<Set<String>> getMobileByRoleIds(@RequestBody Set<Long> roleIds);

    default SecurityUserDTO findByUsername(String username) {
        ResponseData<SecurityUserDTO> result = getByUsername(username);
        if (ResponseData.isSuccess(result)) {
            return result.getData();
        }
        throw new GlobalException(result.getMessage());
    }



    default Set<String> getReceiverByUserIds(Set<String> userIds, String channelType) {
        if (CollUtil.isNotEmpty(userIds)) {
            Set<Long> ids = userIds.stream().map(Long::parseLong).collect(Collectors.toSet());
            ResponseData<Set<String>> responseData = null;
            switch (channelType){
                case "sms" : responseData = getMobileByUserIds(ids);
                    break;
                case "email" : responseData = getEmailByUserIds(ids);
                    break;
                case "im" : responseData = getUserIdsByRoleIds(ids);;
                    break;
                default:
            }
            if (Objects.nonNull(responseData) && ResponseData.isSuccess(responseData)) {
                return responseData.getData();
            }
        }
        return null;
    }

    default Set<String> getReceiverByRoles(Set<String> roleIds, String channelType) {
        if (CollUtil.isNotEmpty(roleIds)) {
            Set<Long> ids = roleIds.stream().map(Long::parseLong).collect(Collectors.toSet());
            ResponseData<Set<String>> responseData = null;
            switch (channelType){
                case "sms" : responseData = getMobileByRoleIds(ids);
                    break;
                case "email" : responseData = getEmailByRoleIds(ids);
                    break;
                case "im" : responseData = getUserIdsByRoleIds(ids);
                    break;
                default:
            }
            if (Objects.nonNull(responseData) && ResponseData.isSuccess(responseData)) {
                return responseData.getData();
            }
        }
        return null;
    }


}
